package eg.com.iti.common.serviceimpl;

import java.util.List;

import eg.com.iti.common.constant.DaoNamesConstants;
import eg.com.iti.common.dao.BaseDao;
import eg.com.iti.common.exception.ITIBusinessException;
import eg.com.iti.common.exception.ITISystemException;
import eg.com.iti.common.factory.DaoFactory;
import eg.com.iti.common.service.BaseService;

public class BaseServiceImpl implements BaseService {

	public void add(Object object) throws ITIBusinessException, ITISystemException {
		// TODO Auto-generated method stub
		BaseDao baseDao = null;
		try {
			baseDao = DaoFactory.createDao(DaoNamesConstants.BASE_DAO);
			baseDao.beginTransaction();
			baseDao.saveOrUpdate(object);
			baseDao.commit();
		} catch (ITISystemException e) {
			// TODO Auto-generated catch block
			baseDao.rollback();
			throw e;
		}
	}

	public void delete(Object object) throws ITIBusinessException, ITISystemException {
		// TODO Auto-generated method stub
		BaseDao baseDao = null;
		try {
			baseDao = DaoFactory.createDao(DaoNamesConstants.BASE_DAO);
			baseDao.beginTransaction();
			baseDao.delete(object);
			//baseDao.commit();
		} catch (ITISystemException e) {
			// TODO Auto-generated catch block
			baseDao.rollback();
			throw e;
		}
	}

	public void update(Object object) throws ITIBusinessException, ITISystemException {
		BaseDao baseDao = null;
		try {
			
			baseDao = DaoFactory.createDao(DaoNamesConstants.BASE_DAO);
			baseDao.beginTransaction();
			baseDao.saveOrUpdate(object);
			baseDao.commit();
			
		} catch (ITISystemException e) {
			// TODO Auto-generated catch block
			baseDao.rollback();
			throw e;
		}		
	}

	public List findAll(Class clazz) throws ITIBusinessException, ITISystemException {
		BaseDao baseDao = null;
		try {
			baseDao = DaoFactory.createDao(DaoNamesConstants.BASE_DAO);			
			baseDao.beginTransaction();	
			List l=baseDao.findAll(clazz);	
			baseDao.commit();
			return l;
		} catch (ITISystemException e) {
			// TODO Auto-generated catch block
			baseDao.rollback();
			throw e;
		}
		
	}

	@Override
	public Object find(Class clazz, Integer id) throws ITIBusinessException, ITISystemException {
		BaseDao baseDao = null;
		try {
			
			baseDao = DaoFactory.createDao(DaoNamesConstants.BASE_DAO);			
			baseDao.beginTransaction();	
			
			Object object=baseDao.find(clazz, id);	
			
			if(object==null)
				System.out.println("null2");
			else 
			baseDao.commit();
			return object;
		} catch (ITISystemException e) {
			baseDao.rollback();
			throw e;
		}
	}
}
